package com.groupseven.happycampus.controller;
import com.groupseven.happycampus.common.pojo.Referee;
import com.groupseven.happycampus.common.result.LayuiPageVo;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.utils.OssUtils;
import com.groupseven.happycampus.common.vo.PieVo;
import com.groupseven.happycampus.service.RefereeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/referee")
public class RefereeController {



    @Autowired
    private RefereeService refereeService;

    /**
     * 分页以及模糊查询所有裁判信息
     * @param params
     * @return
     */
    @RequestMapping("/selectReferee")
    public LayuiPageVo<Referee> selectReferee(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return refereeService.selectReferee(params);
    }




    /**
     * 添加裁判信息
     * @param referee
     * @return
     */

    @RequestMapping("/addReferee")
    public R addReferee(Referee referee) {
        int result = refereeService.addReferee(referee);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }




    /**
     * 根据id删除裁判信息
     * @param id
     * @return
     */
    @GetMapping("/deleteRefereeById")
    public R deleteRefereeById(Integer id){
        int result = refereeService.deleteRefereeById(id);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }


    /**
     * 修改裁判信息
     * @param referee
     * @return
     */

    @RequestMapping("/updateReferee")
    public R updateReferee(Referee referee){
        System.out.println(referee);
        int result = refereeService.updateReferee(referee);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }

    }


    /**
     * 裁判图片上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        //避免预检请求报错  这里有个 if  判断
        if(file!=null){
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            // 将文件转为输入流
            InputStream inputStream = file.getInputStream();

            String url = OssUtils.uploadFile(inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url",url);
        }
        return R.ok();
    }



    /**
     * 裁判级别图
     * @return
     */
    @RequestMapping("/gradePie")
    public R gradePie(){
        List<PieVo> pieVos = refereeService.gradePieData();

        return R.ok().data("gradePie",pieVos);
    }


}
